package com.shengqin.calculate.events.listeners;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson2.JSONObject;
import com.shengqin.calculate.data.DaWeiApiModel;
import com.shengqin.calculate.events.DaWeiApiUploadEvent;
import com.shengqin.controller.dawei.FaceAiClient;
import com.shengqin.controller.dawei.dto.FeatureReq;
import com.shengqin.controller.dawei.dto.FeatureResp;
import com.shengqin.controller.dawei.dto.HeaderParams;
import com.shengqin.controller.dawei.dto.PicInfo;
import com.shengqin.controller.dawei.util.DaWeiHeaderUtil;
import com.shengqin.entity.FaceFeature;
import com.shengqin.entity.PlanStudent;
import com.shengqin.entity.Student;
import com.shengqin.service.IPlanStudentService;
import com.shengqin.service.IStudentService;
import com.shengqin.service.impl.FaceFeatureService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class DaWeiAiListener {

    @Resource
    private FaceFeatureService faceFeatureService;
    @Resource
    private FaceAiClient faceAiClient;

    @SneakyThrows
    @EventListener(DaWeiApiUploadEvent.class)
    @Async
    public void onApplicationEvent(DaWeiApiUploadEvent daWeiApiUploadEvent) {
        DaWeiApiModel model = daWeiApiUploadEvent.getModel();

        try {
            FeatureReq featureReq = new FeatureReq();
            HeaderParams req = DaWeiHeaderUtil.getHeaderParams();
            featureReq.setTimestamp(req.getTimestamp());
            featureReq.setSign(req.getSign());
            PicInfo picInfo = new PicInfo();
            if (!StringUtils.hasLength(model.getBase64()) && model.getFile() != null) {
                String base64Str = Base64.encode(model.getFile());
                picInfo.setBase64(base64Str);
            } else {
                picInfo.setBase64(model.getBase64());
            }
            featureReq.setPic_info(picInfo);
            FeatureResp featureResp = faceAiClient.getFeature(featureReq);

            FaceFeature faceFeature = faceFeatureService.getByStudyCode(model.getStudyCode());
            if(faceFeature == null){
                faceFeature = new FaceFeature();
                faceFeature.setStudyCode(model.getStudyCode());
                faceFeature.setFaceData(Arrays.toString(featureResp.getPic_feature()));
                faceFeature.setFeatureVersion(featureResp.getFeature_version());
                faceFeature.setPicFeatureLen(featureResp.getPic_feature_len());
                faceFeature.setCreateTime(LocalDateTime.now());
            } else {
                faceFeature.setUpdateTime(LocalDateTime.now());
            }

            faceFeatureService.saveOrUpdate(faceFeature);

        } catch (Exception e) {
            log.error("获取大沩人脸识别数据报错", e);
        }
    }
}